package com.my.algorithmlearning.leetcode;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Medium_46_Permutations
 * @Description Medium_46_Permutations
 * @Author wanghaiwei
 * @Date 2023/2/21 10:35
 */
public class Medium_46_Permutations {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> allPermutes = new ArrayList<>();
        List<Integer> currPermute = new ArrayList<>();
        backtrack(nums, allPermutes, currPermute);
        return allPermutes;
    }

    private void backtrack(int[] nums, List<List<Integer>> allPermutes, List<Integer> currPermute) {
        if (currPermute.size() == nums.length) {
            allPermutes.add(new ArrayList<>(currPermute));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (currPermute.contains(nums[i])) {
                continue;
            }
            currPermute.add(nums[i]);
            backtrack(nums, allPermutes, currPermute);
            currPermute.remove(currPermute.size() - 1);
        }
    }

    public static void main(String[] args) {
        Medium_46_Permutations solution = new Medium_46_Permutations();
        List<List<Integer>> result = solution.permute(new int[]{1,2,3});
        result.forEach(r -> System.out.println(StringUtils.join(r, ',')));
        Assert.isTrue(result.size() == 6, "1,2,3计算有误");
    }
}
